/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IPhotoDao;
import org.cx1.dao.impl.PhotoDao;
import org.cx1.model.Listing;
import org.cx1.model.Photo;
import org.cx1.model.Place;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IPhotoService;
import org.cx1.service.api.IPlaceService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class DPhotoService implements IPhotoService {

    private IPhotoDao photoDao = new PhotoDao();
    
    @Override
    public ServiceResponse<String> create(Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Photo entry = new Photo();
            id = IDGenerator.generateID();
            
            entry.setId(id);
            
            entry.setPath(photo.getPath());
            entry.setFilename(photo.getFilename());
            
            entry.setTitle(photo.getTitle());
            entry.setDescription(photo.getDescription());
            entry.setNotes(photo.getNotes());
                                    
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            photoDao.add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<Photo>> readAll(int offset, int limit) {
        ServiceResponse<List<Photo>> response = new ServiceResponse<List<Photo>>();
        List<Photo> photos = new ArrayList<Photo>();
        try {            
            List<Photo> records = photoDao.getAll();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    photos.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    photos.addAll( records.subList(offset, limit) );
                }              
            }            
            response.setResponse(photos);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
    @Override
    public ServiceResponse<Photo> read(String id) {
        ServiceResponse<Photo> response = new ServiceResponse<Photo>();
        Photo photo = null;       
        try {            
            photo = photoDao.getById(id);      
            response.setResponse(photo);
            response.setSuccessful(true);           
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            photoDao.deleteById(id);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<String> createUserPhoto(String userID, Photo photo) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteUserPhoto(String userID, String photoID) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> createListingPhoto(String listingID, Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            photoID = this.create(photo).getResponse();
            if (!StringUtils.isEmpty(photoID)) {
                photoDao.addListingPhoto(listingID, photoID);
                response.setSuccessful(true);                   
            }        
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteListingPhoto(String listingID, String photoID) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            photoDao.deleteListingPhoto(listingID, photoID);
            success = true;
            response.setResponse(success);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteListingPhoto(String listingID, String photoID, String userID) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            IListingService listingService = new DListingService();
            ServiceResponse<Listing> response1 = listingService.read(listingID);
            Listing listing = response1.getResponse();
            
            if (StringUtils.equalsIgnoreCase(listing.getCreatorID(), userID)) {
                photoDao.deleteListingPhoto(listingID, photoID);
                success = true;
                response.setResponse(success);                
            }
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    @Override
    public ServiceResponse<String> createPlacePhoto(String placeID, Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            photoID = this.create(photo).getResponse();
            if (!StringUtils.isEmpty(photoID)) {
                photoDao.addPlacePhoto(placeID, photoID);
                response.setSuccessful(true);                   
            }        
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deletePlacePhoto(String placeID, String photoID) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            photoDao.deletePlacePhoto(placeID, photoID);
            success = true;
            response.setResponse(success);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    
    @Override
    public ServiceResponse<String> createBlogPhoto(String blogID, Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            photoID = this.create(photo).getResponse();
            if (!StringUtils.isEmpty(photoID)) {
                photoDao.addBlogPhoto(blogID, photoID);
                response.setSuccessful(true);                   
            }        
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteBlogPhoto(String blogID, String photoID) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            photoDao.deleteBlogPhoto(blogID, photoID);
            success = true;
            response.setResponse(success);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    public ServiceResponse<String> createImport(Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Photo entry = new Photo();
            id = photo.getId();
            
            entry.setId(id);
            
            entry.setPath(photo.getPath());
            entry.setFilename(photo.getFilename());
            
            entry.setTitle(photo.getTitle());
            entry.setDescription(photo.getDescription());
            entry.setNotes(photo.getNotes());
                                    
            entry.setActive(photo.isActive());
            entry.setDateCreated(photo.getDateCreated());
            entry.setDateModified(photo.getDateModified());
                        
            photoDao.add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    public static void main (String[] argv) {
        IPhotoService photoService = new DPhotoService();
        
        /*
        for (int i = 1; i < 11; i++) {
            Photo photo = new Photo();
            photo.setId(IDGenerator.generateID());
            photo.setFilename("photo-"+i);
            photoService.create(photo);
        }
        */
        
        /*
        ServiceResponse<List<Photo>> response1 = photoService.readAll(0, 0);
        List<Photo> photos = response1.getResponse();
        if (photos != null && photos.size() > 0) {
            for (Photo photo : photos) {
                System.out.println("CX: " + photo.getFilename() + " - " + photo.getId());
            }
        }
        */
        
        /*
        ServiceResponse<Photo> response1 = photoService.read("060bd6cd82");
        Photo photo = response1.getResponse();
        if (photo != null) {
            System.out.println("CX: " + photo.getId());
            System.out.println("CX: " + photo.getFilename());
        }      
        */
        
        /*
        ServiceResponse<Boolean> response1 = photoService.delete("219d6a7f39");           
        ServiceResponse<Photo> response2 = photoService.read("219d6a7f39");        
        Photo photo = response2.getResponse();
        if (photo == null) {
            System.out.println("YAY");
        }    
        */
        
        
        
        String placeID = "2d7e3c6d29";
        for (int i = 1; i < 11; i++) {
            Photo photo = new Photo();
            photo.setId(IDGenerator.generateID());
            photo.setFilename("photo-"+i);
            photoService.createPlacePhoto(placeID, photo);
        }
        
    }

}
